package com.spring.sunflower.common;

import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.spring.sunflower.R;
import com.spring.sunflower.base.BaseActivity;
import com.spring.sunflower.bean.LabelBean;
import com.spring.sunflower.bean.OnlineStatusBean;
import com.spring.sunflower.constant.Constant;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Kevin on 2021/6/28<br/>
 * <p>
 * Describe:<br/>
 */
public class OnlineStatusActivity extends BaseActivity<OnlineStatusPresenter> implements CommonContract.OnlineStatusView {
    private RecyclerView mRecyclerView;
    private OnlineStatusAdapter mAdapter;
    private List<OnlineStatusBean> mData = new ArrayList<>();
    private int clickPos = -1;

    @Override
    public void initView() {
        tvTitle.setText("在线状态");
        mRecyclerView = findViewById(R.id.rvRecyclerView);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mAdapter = new OnlineStatusAdapter(R.layout.adapter_online_status, mData);
        mRecyclerView.setAdapter(mAdapter);
        mPresenter.getOnlineStatus();
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                OnlineStatusBean onlineStatusBean = mData.get(position);
                if (!onlineStatusBean.isChecked()) {
                    clickPos = position;
                    mPresenter.setOnlineStatus(onlineStatusBean.getId());
                }
            }
        });
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_online_status;
    }

    @Override
    public OnlineStatusPresenter setPresenter() {
        return new OnlineStatusPresenter(this);
    }

    @Override
    public void showOnlineStatus(List<LabelBean.DatasBean> data) {
        List<OnlineStatusBean> onlineStatusData = new ArrayList<>();
        String onLineStatus = getField(Constant.KEY_ONLINE_STATUS, "");
        for (int i = 0; i < data.size(); i++) {
            OnlineStatusBean onlineStatusBean = new OnlineStatusBean();
            if (TextUtils.isEmpty(onLineStatus)) {
                if (i == 0) {
                    onlineStatusBean.setChecked(true);
                } else {
                    onlineStatusBean.setChecked(false);
                }
            } else if (data.get(i).getId().equals(onLineStatus)) {
                onlineStatusBean.setChecked(true);
            } else {
                onlineStatusBean.setChecked(false);
            }
            onlineStatusBean.setColor(data.get(i).getColor());
            onlineStatusBean.setId(data.get(i).getId());
            onlineStatusBean.setName(data.get(i).getName());
            onlineStatusData.add(onlineStatusBean);
        }
        mAdapter.addData(onlineStatusData);
    }

    @Override
    public void setOnlineStatusSuccess() {
        if (clickPos >= 0) {
            for (int i = 0; i < mData.size(); i++) {
                if (clickPos == i) {
                    saveField(Constant.KEY_ONLINE_STATUS, mData.get(i).getId());
                    mData.get(i).setChecked(true);
                } else {
                    mData.get(i).setChecked(false);
                }
            }
//            mAdapter.setNewInstance(mData);
            mAdapter.notifyDataSetChanged();
            showToast("修改成功");
        }
    }

    @Override
    public void setOnlineStatusFailed() {
        clickPos = -1;
    }
}
